/**
 * Project Name:mauto-web
 * File Name:CompanyController.java
 * Package Name:com.mingsoft.company.action
 * Date:2017年6月13日上午10:46:18
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.jy.web;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.jy.entity.JyEnterprise;
import com.inftech.mauto.jy.entity.Technician;
import com.inftech.mauto.jy.service.JyEnterpriseService;
import com.inftech.mauto.jy.service.TechnicianService;
import com.mingsoft.base.action.BaseAction;
import com.mingsoft.basic.constant.e.SessionConstEnum;
import com.mingsoft.basic.entity.ManagerSessionEntity;

import net.mingsoft.basic.util.BasicUtil;

/**
 * ClassName:CompanyController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Date: 2017年6月13日 上午10:46:18 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/${managerPath}/company")
public class CompanyAction extends BaseAction{
	Log log = LogFactory.getLog(CompanyAction.class);
	/**
	 * 企业Service
	 */
	@Resource(name = "jyEnterpriseBizImpl")
	private JyEnterpriseService jyEnterpriseBizImpl;
	/**
	 * 技师Service
	 */
	@Resource
	private TechnicianService technicianBizImpl;
	/**
	 * 
	 * list:(企业列表). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = { "list.do" })
	public String list(HttpServletRequest request, HttpServletResponse response) {
		// 调用PageHelper分页
		BasicUtil.startPage();
		String enterpriseName = request.getParameter("enterpriseName")==null ? "": request.getParameter("enterpriseName").trim();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("enterpriseName", enterpriseName);
		List<JyEnterprise> jyEnterpriseList = jyEnterpriseBizImpl.getJyEnterpriseList(map);
		BasicUtil.endPage(jyEnterpriseList);
		request.setAttribute("jyEnterpriseList", jyEnterpriseList);
		request.setAttribute("enterpriseName", enterpriseName);
		request.setAttribute("pageNo",
				CommonUtils.isBlank(request.getParameter("pageNo")) ? 1 : request.getParameter("pageNo"));
		return "/WEB-INF/manager/mauto/jy/company";
	}
	
	/**
	 * 
	 * getTechnicianlistById:(). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/getTechnicianlistById.do")
	public String  getTechnicianlistById(HttpServletRequest request, HttpServletResponse response){
		BasicUtil.startPage();
		String id = request.getParameter("id")==null ? "": request.getParameter("id").trim();
		List<Technician> technicianlist = technicianBizImpl.getTechnicianlistById(id);
		BasicUtil.endPage(technicianlist);
		request.setAttribute("technicianlist", technicianlist);
		return "/WEB-INF/manager/mauto/jy/companyTechnicianlist";
	}
	
	/**
	 * 
	 * save:(新增企业). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/save.do")
	public String save(HttpServletRequest request,HttpServletResponse response){
		ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
				SessionConstEnum.MANAGER_SESSION);
		Enumeration<String> enumeration = request.getParameterNames();
		String name = null;
		String value = null;
		Map<String,Object> map = new HashMap<String,Object>();
		while(enumeration.hasMoreElements()){
			name = enumeration.nextElement();
			map.put(name, request.getParameter(name)==null ? null :request.getParameter(name).trim() );
		}
		map.put("createBy", managerSession.getManagerId());
		try{
			jyEnterpriseBizImpl.save(map);
			map.put("result", "true");
		}catch(Exception e){
			log.error(e);
			map.put("resultMsg", "系统异常！");
		}
		JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
		outJson(response, jsonObject);
		return null;
	}
	/**
	 * 
	 * allDelete:(删除企业). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/allDelete.do")
	public String allDelete(HttpServletRequest request, HttpServletResponse response){
		Map<String,Object> map = new HashMap<String,Object>();
		Enumeration<String> enumeration = request.getParameterNames();
		String name = null;
		List<String> list = new ArrayList<String>();
		while(enumeration.hasMoreElements()){
			name = enumeration.nextElement();
			list.add(request.getParameter(name));
		}
		List<Map<String,Object>> list2 = technicianBizImpl.getCountByCompanyId(list);
		for(Map<String,Object> map1:list2){
			if(!"0".equals(map1.get("count").toString())){
				map.put("result", "false");
				JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
				outJson(response, jsonObject);
				return null;
			}
		}
		int row = jyEnterpriseBizImpl.deleteAll(list);
		
		if(row>0){
			map.put("result", "true");
		}
		JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
		outJson(response, jsonObject);
		return null;
	}
}
